package com.cyf.mq.consumer;

import com.cyf.mq.constant.MqConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author chenyifan
 * @create 2025-02-18 17:43
 */
@Slf4j
public class KeyConsumer {

    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(MqConstant.MESSAGE_CONSUMER_GROUP_B);
        consumer.setNamesrvAddr(MqConstant.NAME_SERVER_ADDR);
        consumer.subscribe(MqConstant.MESSAGE_TOPIC_King, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext context) {
                for (MessageExt ext : list) {
                    log.info("received msg, messageId:{}, topic:{}, key:{}, body:{}", ext.getMsgId(), ext.getTopic(), ext.getKeys(), new String(ext.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.in.read();
    }
}
